var ClassList = function () {
};
var n = /(~|\^+)?-?[_0-9a-z][-_0-9a-z]*/gi;
var a = null;
ClassList._setElementSystem = function (e) {
    a = e;
    ClassList._setElementSystem = null;
};
var hasOwnProperty = Object.prototype.hasOwnProperty;
ClassList.create = function (e, t) {
    var n = new ClassList();
    for (var a in (n._prefix = '', n._addOriginalClass = false, n._alias = t, n._resolvedAlias = {}, t))
        n._resolvedAlias[a] = '';
    n._rawNames = [];
    n._elem = e;
    n._owner = null;
    return n;
};
var o = function (e) {
    var t = '';
    var i = e._rawNames;
    var n = e._prefix;
    var a = e._owner ? e._owner._resolvedAlias : null;
    var o = e._addOriginalClass;
    n && (n += '--');
    for (var s = 0; s < i.length; s++) {
        var l = i[s];
        s && (t += ' ');
        if (a && hasOwnProperty.call(a, l))
            t += a[l];
        else if ('~' === l[0])
            t += l.slice(1);
        else if ('^' === l[0]) {
            l = l.slice(1);
            for (var d = e._owner || null; '^' === l[0];)
                l = l.slice(1), d = d ? d._owner : null;
            var A = d ? d._prefix : '';
            A && (A += '--');
            t += A + l;
        } else
            o && (t += l + ' '), t += n + l;
    }
    return t;
};
var s = function (e) {
    var t = e._elem.__domElement;
    if (t) {
        var i = o(e);
        i ? t.setAttribute('class', i) : t.removeAttribute('class');
    }
};
var l = function (e) {
    var t = e.classList;
    if (t instanceof ClassList) {
        t._alias && d(t);
        s(t);
    };
    var n = e.childNodes;
    for (var r = 0; r < n.length; r++)
        n[r] instanceof a && l(n[r]);
};
var d = function (e) {
    var t = e._owner;
    if (!t)
        return false;
    var i = false;
    for (var n in e._alias) {
        var o = e._alias[n];
        var s = [];
        if (o)
            for (var d = 0; d < o.length; d++) {
                var A = o[d];
                if (hasOwnProperty.call(t._alias, A)) {
                    var c = t._resolvedAlias[A];
                    s.push(c);
                } else {
                    var h = e._prefix && A ? e._prefix + '--' + A : A;
                    s.push(h);
                }
            }
        var u = s.join(' ');
        if (e._resolvedAlias[n] !== u) {
            i = true;
            e._resolvedAlias[n] = u;
        };
    }
    i && e._elem.shadowRoot instanceof a && l(e._elem.shadowRoot);
};
ClassList.prototype.toggle = function (e, t) {
    var i = this._rawNames.indexOf(e);
    undefined === t && (t = i < 0);
    t ? i < 0 && (this._rawNames.push(e), s(this)) : i >= 0 && (this._rawNames.splice(i, 1), s(this));
};
ClassList.prototype.contains = function (e) {
    var t = this._rawNames;
    for (var i = 0; i < t.length; i++) {
        var n = t[i];
        if ('~' === n[0]) {
            if (n.slice(1) === e)
                return true;
        } else if ('^' === n[0]) {
            for (var a = 1; '^' === n[a];)
                a++;
            if (n.slice(a) === e)
                return true;
        } else if (n === e)
            return true;
    }
    return false;
};
ClassList.prototype._setOwnerOptions = function (e, t, i) {
    var n = this._prefix;
    this._prefix = t;
    n !== t && (this._addOriginalClass = i, this._owner = e, this._alias && d(this), s(this));
};
ClassList.prototype._setAlias = function (e, t) {
    var i = this._owner;
    var a = String(t).match(n);
    this._alias[e] = a;
    var o = [];
    if (a)
        for (var s = 0; s < a.length; s++) {
            var d = a[s];
            i && hasOwnProperty.call(i._alias, d) ? o.push(i._resolvedAlias[d]) : o.push(this._prefix ? this._prefix + '--' + d : d);
        }
    this._resolvedAlias[e] = o.join(' ');
    l(this._elem.shadowRoot);
};
ClassList.prototype.setClassNames = function (e) {
    e = null == e ? '' : String(e);
    this._rawNames = e.match(n) || [];
    s(this);
};
ClassList.prototype.getClassNames = function () {
    return o(this);
};
ClassList.prototype.getPrefix = function () {
    return this._prefix;
};
ClassList.prototype.getAddOriginalClass = function () {
    return this._addOriginalClass;
};
ClassList.prototype.getRawNames = function () {
    return this._rawNames;
};
module.exports = ClassList;
